package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.OliveAssetsBorrowMapper;
import com.ruoyi.system.mapper.OliveAssetsMapper;
import com.ruoyi.system.mapper.OliveAssetsRepairMapper;
import com.ruoyi.system.service.IOliveAssetsBorrowService;
import com.ruoyi.system.service.IOliveAssetsRepairService;
import com.ruoyi.system.service.IOliveAssetsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 基础信息管理Service业务层处理
 *
 * @author hzl
 * @date 2024-12-08
 */
@Service
public class OliveAssetsRepairServiceImpl implements IOliveAssetsRepairService
{
    @Autowired
    private OliveAssetsRepairMapper oliveAssetsRepairMapper;

    /**
     * 查询基础信息管理
     *
     * @param assetId 基础信息管理主键
     * @return 基础信息管理
     */
    @Override
    public OliveAssets selectOliveAssetsByAssetId(String assetId)
    {
        return oliveAssetsRepairMapper.selectOliveAssetsByAssetId(assetId);
    }

    /**
     * 查询基础信息管理列表
     *
     * @param oliveAssets 基础信息管理
     * @return 基础信息管理
     */
    @Override
    public List<OliveAssets> selectOliveAssetsList(OliveAssets oliveAssets)
    {
        return oliveAssetsRepairMapper.selectOliveAssetsList(oliveAssets);
    }

    /**
     * 新增基础信息管理
     *
     * @param oliveAssets 基础信息管理
     * @return 结果
     */
    @Override
    public int insertOliveAssets(OliveAssets oliveAssets)
    {
        return oliveAssetsRepairMapper.insertOliveAssets(oliveAssets);
    }

    /**
     * 修改基础信息管理
     *
     * @param oliveAssets 基础信息管理
     * @return 结果
     */
    @Override
    public int updateOliveAssets(OliveAssets oliveAssets)
    {
        return oliveAssetsRepairMapper.updateOliveAssets(oliveAssets);
    }

    /**
     * 批量删除基础信息管理
     *
     * @param assetIds 需要删除的基础信息管理主键
     * @return 结果
     */
    @Override
    public int deleteOliveAssetsByAssetIds(String[] assetIds)
    {
        return oliveAssetsRepairMapper.deleteOliveAssetsByAssetIds(assetIds);
    }

    /**
     * 删除基础信息管理信息
     *
     * @param assetId 基础信息管理主键
     * @return 结果
     */
    @Override
    public int deleteOliveAssetsByAssetId(String assetId)
    {
        return oliveAssetsRepairMapper.deleteOliveAssetsByAssetId(assetId);
    }

    @Override
    public List<TopFourAsset> getTopFourAssets() {
        return oliveAssetsRepairMapper.getTopFourAssets();
    }

    @Override
    public RestAssets getRestAssets() {
        return oliveAssetsRepairMapper.getRestAssets();
    }

    @Override
    public List<TopFourAsset> getTopFourAssetsByPlantArea(OliveAssets oliveAssets) {
        int plantAreaId = Math.toIntExact(oliveAssets.getPlantAreaId());
        return oliveAssetsRepairMapper.getTopFourAssetsByPlantArea(plantAreaId);
    }

    @Override
    public RestAssets getRestAssetsByPlantArea(OliveAssets oliveAssets) {
        int plantAreaId = Math.toIntExact(oliveAssets.getPlantAreaId());
        return oliveAssetsRepairMapper.getRestAssetsByPlantArea(plantAreaId);
    }

    @Override
    public String importAssets(List<OliveAssets> assetList, boolean updateSupport, String operName) {
        if (assetList == null || assetList.isEmpty()) {
            return "导入数据为空";
        }

        int successCount = 0;
        int failCount = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failMsg = new StringBuilder();

        for (OliveAssets asset : assetList) {
            try {
                // 检查是否存在相同 assetId 的记录
                OliveAssets existingAsset = oliveAssetsRepairMapper.selectOliveAssetsByAssetId(asset.getAssetId());
                if (existingAsset != null) {
                    if (updateSupport) {
                        // 更新记录
                        asset.setOriginaletAssetId(asset.getAssetId());
                        int updateResult = oliveAssetsRepairMapper.updateOliveAssets(asset);
                        if (updateResult > 0) {
                            successCount++;
                            successMsg.append("更新成功: ").append(asset.getAssetId()).append("<br/>");
                        } else {
                            failCount++;
                            failMsg.append("更新失败: ").append(asset.getAssetId()).append("<br/>");
                        }
                    } else {
                        failCount++;
                        failMsg.append("已存在相同 assetId 的记录，未更新: ").append(asset.getAssetId()).append("<br/>");
                    }
                } else {
                    // 插入新记录
                    int insertResult = oliveAssetsRepairMapper.insertOliveAssets(asset);
                    if (insertResult > 0) {
                        successCount++;
                        successMsg.append("插入成功: ").append(asset.getAssetId()).append("<br/>");
                    } else {
                        failCount++;
                        failMsg.append("插入失败: ").append(asset.getAssetId()).append("<br/>");
                    }
                }
            } catch (Exception e) {
                failCount++;
                failMsg.append("导入失败: ").append(asset.getAssetId()).append("，原因: ").append(e.getMessage()).append("<br/>");
            }
        }

        String resultMsg = "成功导入 " + successCount + " 条数据，失败 " + failCount + " 条数据";
        if (successCount > 0) {
            resultMsg += "<br/>成功信息: <br/>" + successMsg.toString();
        }
        if (failCount > 0) {
            resultMsg += "<br/>失败信息: <br/>" + failMsg.toString();
        }

        return resultMsg;
    }

    @Override
    public int insertOliveAssetsRepair(OliveAssetsRepair oliveAssetsRepair) {
        return oliveAssetsRepairMapper.insertOliveAssetsRepair(oliveAssetsRepair);
    }

}
